package vn.isolar.util.direction;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import vn.isolar.object.DetailRouting;

public class Direction {
	private String jsonResult;

	public DetailRouting getDetailRooter(String urlmap) {
		String url = urlmap;
		try {
			jsonResult = fetchData(url);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		ArrayList<Bundle> list_steps = new ArrayList<Bundle>();
		ArrayList<Bundle> list_global = new ArrayList<Bundle>();
		try {
			JSONObject json = new JSONObject(jsonResult);
			JSONArray routes = json.getJSONArray("routes");
			// Grab the first route
			JSONObject route = routes.getJSONObject(0);
			// Take all legs from the route
			JSONArray legs = route.getJSONArray("legs");
			JSONObject legs_object = legs.getJSONObject(0);
			JSONArray steps = legs_object.getJSONArray("steps");
			
			list_steps = getListStep(steps);
			list_global = getListGlobal(legs_object);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return new DetailRouting(list_steps, list_global);
		
	}
	public ArrayList<Bundle> getListGlobal(JSONObject legs_object) throws JSONException{
		ArrayList<Bundle> list_global = new ArrayList<Bundle>();
		JSONObject duration = legs_object.getJSONObject("duration");
		Bundle dur = new Bundle();
		dur.putString("text", duration.getString("text"));
		dur.putString("value", duration.getString("value"));
		JSONObject distance = legs_object.getJSONObject("distance");
		
		Bundle dis = new Bundle();
		dis.putString("text", distance.getString("text"));
		dis.putString("value", distance.getString("value"));
		
		String start_address = legs_object.getString("start_address");
		Bundle start_addr = new Bundle();
		start_addr.putString("start_address", start_address);
		
		
		String end_address = legs_object.getString("end_address");
		Bundle end_addr = new Bundle();
		end_addr.putString("end_address", end_address);
		
		list_global.add(dur);
		list_global.add(dis);
		list_global.add(start_addr);
		list_global.add(end_addr);
		return list_global;
	}
	public ArrayList<Bundle> getListStep(JSONArray steps) throws JSONException{
		ArrayList<Bundle> list_steps = new ArrayList<Bundle>();
		for (int i = 0; i < steps.length(); i++) {
			JSONObject singleStep = steps.getJSONObject(i);
			JSONObject duration = singleStep.getJSONObject("duration");
			Bundle dur = new Bundle();
			dur.putString("text", duration.getString("text"));
			dur.putString("value", duration.getString("value"));
			
			
			JSONObject distance = singleStep.getJSONObject("distance");
			Bundle dis = new Bundle();
			dis.putString("text", distance.getString("text"));
			dis.putString("value", distance.getString("value"));
			
			String html_instructions = singleStep.getString("html_instructions");
			String instructions = android.text.Html.fromHtml(html_instructions).toString(); 
			
			JSONObject start_location = singleStep.getJSONObject("start_location");
			Bundle start_point = new Bundle();
			start_point.putDouble("lng", start_location.getDouble("lng"));
			start_point.putDouble("lat", start_location.getDouble("lat"));
			
			JSONObject end_location = singleStep.getJSONObject("end_location");
			Bundle end_point = new Bundle();
			start_point.putDouble("lng", end_location.getDouble("lng"));
			start_point.putDouble("lat", end_location.getDouble("lat"));
			
			Bundle data = new Bundle();
			data.putBundle("duration", dur);
			data.putBundle("distance", dis);
			data.putString("instructions", instructions);
			data.putBundle("start_location", start_point);
			data.putBundle("end_location", end_point);
			
			list_steps.add(data);
		}
		return list_steps;
	}
	public String fetchData(String urlmap) throws IOException {
		String stringUrl = urlmap;

		URL url = new URL(stringUrl);
		HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
		StringBuilder response = new StringBuilder();
		if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					httpconn.getInputStream()), 8192);
			String strLine = null;

			while ((strLine = input.readLine()) != null) {
				response.append(strLine);
			}
			input.close();
		}
		return response.toString();

	}

	public DetailRouting getDirectionDetail(String srcAddr,
			String desAddr) {
		try {
			srcAddr = URLEncoder.encode(srcAddr, "utf-8");
			desAddr = URLEncoder.encode(desAddr, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String url = "http://maps.googleapis.com/maps/api/directions/json?" +
				"origin="+srcAddr +
				"&destination="+desAddr +
				"&sensor=true" +
				"&language=vi";
		// TODO Auto-generated method stub
		Log.w("url",url);
		return getDetailRooter(url);
	}
}
